// pages/Recharge/Recharge.js
var util = require('../../utils/util.js')
var app = getApp()
var flags = true
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myMoney:0.00,
    submitload: false,	//提交Loading状态
    infoList: [],
    rid:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取我的余额
    this.getMyMoney()
    //获取充值模块
    this.getRechargeModel()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
	app.publicchangenavigate()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
	return app.globalshare();
  },
  /**
   * 获取我的余额
   */
  getMyMoney:function(){
    var that = this
    var uid = app.globalData.userInfoId
    //开启加载
    wx.showLoading({
      title: '加载中',
      mask: true,
    })
    //提交给服务端注册信息
    util.AJAX('Home/Recharge/getMyMoney', { uid: uid }, function (res) { //方法体
      //关闭加载
      wx.hideLoading()

      if (res.data.StatusCode == 200) {  //成功
        //wx.removeStorageSync('ist')
        if (res.data.Moneys==null){
          var Moneys = 0.00
        }else{
          var Moneys = res.data.Moneys
        }
        that.setData({ myMoney: Moneys })
      } else {	//失败

        wx.showModal({
          title: '提示',
          content: res.data.Msg
        })
      }
    }, 'POST')
  },
  /**
   * 获取充值模块
   */
  getRechargeModel:function(){
    var that = this;
    //开启加载
    wx.showLoading({
      title: '加载中',
      mask: true,
    })
    //提交给服务端注册信息
    util.AJAX('Home/Recharge/getRechargeModel', { uid: app.globalData.userInfoId }, function (res) { //方法体
      //关闭加载
      wx.hideLoading()

      if (res.data.StatusCode == 200) {  //成功
        //wx.removeStorageSync('ist')
        that.setData({ infoList: res.data.Info })
      } else {	//失败

        wx.showModal({
          title: '提示',
          content: res.data.Msg
        })
      }
    }, 'POST')
  },
  /**
   * 选择充值模板
   */
  RechargeModelTap:function(e){
    var rid = e.currentTarget.dataset.rid
    var infoList = this.data.infoList
    for (var i = 0; i < infoList.length;i++){
      if (infoList[i].id==rid){
        infoList[i].ist = 1
      }else{
        infoList[i].ist = 0
      }
    }
    this.setData({ infoList: infoList })
    this.setData({ rid: rid })
  },
  /**
   * 充值
   */
  RechargeTj:function(){
    var that = this
    var rid = that.data.rid
    //var uid = app.globalData.userInfoId
    var uid = app.globalData.userInfoId
    if(uid==null){
      wx.showModal({
        title: '提示',
        content: "抱歉，您还未登录！"
      })
      return false
    }
    if (rid == 0) {
      wx.showModal({
        title: '提示',
        content: "请选择充值的面额！"
      })
      return false
    }

    if(flags){
      flags = false
      //开启加载
      wx.showLoading({
        title: '加载中',
        mask: true,
      })
      //提交给服务端    模拟充值
      util.AJAX('Home/Recharge/Recharge', { uid: uid,rid:rid }, function (ress) { //方法体
        //关闭加载
        wx.hideLoading()
        if (ress.data.StatusCode == 200) {  //成功
          //发起支付
          wx.requestPayment({
            'timeStamp': ress.data.Data.timeStamp,
            'nonceStr': ress.data.Data.nonceStr,
            'package': ress.data.Data.package,
            'signType': ress.data.Data.signType,
            'paySign': ress.data.Data.paySign,
            'success': function (res) {
              wx.showToast({
                title: '支付成功',
                icon: 'success',
                duration: 3000
              });
              flags = true
              that.getMyMoney()
              wx.setStorageSync('rechargeist',1)
              app.publicnavigateTologing('/pages/Recharge/RechargeSucess/RechargeSucess?money=' + ress.data.money + '&actualmoney=' + ress.data.actualmoney)
            },
            'fail': function (res) {
              flags = true
            }
          })
        } else {	//失败
          flags = true
          wx.showModal({
            title: '提示',
            content: res.data.Msg
          })
        }
      }, 'POST')
    }else{
      return false
    }
  }
})